package nz.ac.massey.spidernetpn.database.tests;


import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import nz.ac.massey.spidernetpn.database.GraphDatabase;
import nz.ac.massey.spidernetpn.graph.SPNGraph;

import org.junit.*;

import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;


public class DatabaseTestCases {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		testGraph = null;
		//System.out.println("In setup");
		URL node1 = new URL("http://www.massey.ac.nz");
		URL node2 = new URL("https://alumnionline.massey.ac.nz/");
		URL node3 = new URL("https://secure.mymassey.com/");
		
		URL node4 = new URL("http://alumnionline.massey.ac.nz/NetCommunity/Page.aspx?pid=334");
		URL node5 = new URL("http://masseynews/massey/about-us/news/print-publications/definingnz.cfm");
		
		testGraph = new DirectedSparseMultigraph<URL, Integer>();
		
		testGraph.addEdge(0, node1, node1);
		testGraph.addEdge(1, node1, node2);
		testGraph.addEdge(2, node1, node3);
		testGraph.addEdge(3, node2, node4);
		testGraph.addEdge(4, node2, node5);
	
	}
	
	private DirectedGraph<URL, Integer> testGraph;
	
	//New Save graph
	@Test
	public void test1(){
		String fileName = "Test.xml";

		String fname = System.getProperty("user.dir")+"/test/Graphs/";
	    // A File object to represent the filename
	    File f = new File(fname+fileName);
	    if(f.exists()){
	    	assertTrue(f.delete());
	    }
	    
		GraphDatabase testDB = new GraphDatabase();

		try {
			assertTrue(testDB.saveGraph("Test", testGraph));
		} catch (Exception e) {
			assertTrue(false);
		}

	}
	
	//Load Graph
	@Test
	public void test2(){
	
		GraphDatabase testDB = new GraphDatabase();
		DirectedGraph<URL, Integer> testGraph2 = null;
		try {
			testGraph2 = testDB.loadCustomGraph("Test.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}	
		
		
		System.out.println("Graph 1 "+testGraph2 );
		System.out.println("Graph 2 "+testGraph );
		
		assertTrue(testGraph2.getEdgeCount()==5);
		assertTrue(testGraph2.getVertexCount()==5);
		
		assertTrue(testGraph2.containsEdge(0));
		assertTrue(testGraph2.containsEdge(4));
		assertTrue(!testGraph2.containsEdge(5));
		
		try {
			assertTrue(testGraph2.findEdge(new URL("http://www.massey.ac.nz"), new URL("http://www.massey.ac.nz"))==0);
			assertTrue(testGraph2.findEdge(new URL("https://alumnionline.massey.ac.nz/"), new URL("http://masseynews/massey/about-us/news/print-publications/definingnz.cfm"))==4);
			assertTrue(testGraph2.findEdge(new URL("http://www.massey.ac.nz"), new URL("http://www.NOTASITE.ac.nz"))==null);
			
			
			assertTrue(testGraph2.containsVertex(new URL("http://www.massey.ac.nz")));
			assertTrue(testGraph2.containsVertex(new URL("http://masseynews/massey/about-us/news/print-publications/definingnz.cfm")));
			assertTrue(!testGraph2.containsVertex(new URL("http://notonlistsite.com")));
		} catch (MalformedURLException e) {
			assertTrue(false);
		}
		
	}
	

	@Test
	public void test3(){	
		GraphDatabase testDB = new GraphDatabase();	
		List testList = testDB.getListOfStoredGraphs();	
		assertTrue(testList.size()>0);
		assertTrue(testList.contains("Test.xml"));
		assertTrue(!testList.contains("SOMEINCROREXsdfNoonenose.xl"));
	}
	

	@After
	public void tearDown() throws Exception {
	}

}
